import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

class Robot {
    private final static ThreadLocal<Map<Orientation, Orientation>> TURN_RIGHT_MAP = ThreadLocal.withInitial(
            () -> new HashMap<>() {
                {
                    put(Orientation.EAST, Orientation.SOUTH);
                    put(Orientation.SOUTH, Orientation.WEST);
                    put(Orientation.WEST, Orientation.NORTH);
                    put(Orientation.NORTH, Orientation.EAST);
                }
            });

    private final static ThreadLocal<Map<Orientation, Orientation>> TURN_LEFT_MAP = ThreadLocal.withInitial(
            () -> new HashMap<>() {
                {
                    put(Orientation.EAST, Orientation.NORTH);
                    put(Orientation.NORTH, Orientation.WEST);
                    put(Orientation.WEST, Orientation.SOUTH);
                    put(Orientation.SOUTH, Orientation.EAST);
                }
            });

    private final static ThreadLocal<Map<Orientation, Consumer<Robot>>> ADVANCE_MAP = ThreadLocal.withInitial(
            () -> new HashMap<>() {
                {
                    put(Orientation.EAST, (robot) -> robot.gridPosition = new GridPosition(robot.gridPosition.x + 1, robot.gridPosition.y));
                    put(Orientation.SOUTH, (robot) -> robot.gridPosition = new GridPosition(robot.gridPosition.x, robot.gridPosition.y - 1));
                    put(Orientation.WEST, (robot) -> robot.gridPosition = new GridPosition(robot.gridPosition.x - 1, robot.gridPosition.y));
                    put(Orientation.NORTH, (robot) -> robot.gridPosition = new GridPosition(robot.gridPosition.x, robot.gridPosition.y + 1));
                }
            });

    private final static ThreadLocal<Map<String, Consumer<Robot>>> SIMULATE_MAP = ThreadLocal.withInitial(
            () -> new HashMap<>() {
                {
                    put("R", Robot::turnRight);
                    put("L", Robot::turnLeft);
                    put("A", Robot::advance);
                }
            });

    private GridPosition gridPosition;
    private Orientation orientation;

    Robot(GridPosition initialGridPosition, Orientation north) {
        this.gridPosition = initialGridPosition;
        this.orientation = north;
    }

    GridPosition getGridPosition() {
        return this.gridPosition;
    }

    Orientation getOrientation() {
        return this.orientation;
    }

    void turnRight() {
        this.orientation = Robot.TURN_RIGHT_MAP.get().get(this.orientation);
    }

    void turnLeft() {
        this.orientation = Robot.TURN_LEFT_MAP.get().get(this.orientation);
    }

    void advance() {
        Robot.ADVANCE_MAP.get().get(this.orientation).accept(this);
    }

    void simulate(final String commands) {
        for (String command : commands.split("")) {
            Robot.SIMULATE_MAP.get().get(command).accept(this);
        }
    }
}
